﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Entity;
using Job.Logic.Common;
using Job.Web.Framework;
using Job.Logic.BaseContent;

namespace Job.Web.userControls.Admin
{
    public partial class HunterJobInfo : BaseCtrl
    {
        HunterJobLogic logic = new HunterJobLogic(); 

        private int jobId
        {
            get { return myR["jobId"].IntValue; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (jobId > 0)
                {
                    JobHunter job = logic.GetDetail(jobId);
                    if (null != job) show(job);
                }
                else
                {
                    init();
                }
            }
        }

        private void init()
        {
            lbSequence.Text = "0";
            tbJobNo.Text = "";
            tbJobName.Text = "";
            //tbIndustry.Text = "";
            //tbSalary.Text = "";
            WebCommon.SetDropDownFromDict(ddlSalary, "",
                Constants.GetDictionaryCode(Constants.DictionaryType.SALARY_LEVEL), false);

            WebCommon.LoadLevelDropDownList(ddlAddrFather, ddlAddrSub, AddressRate,
                "","", Constants.DictionaryType.ADDR_CITY);
            WebCommon.LoadLevelDropDownList(ddlIndustryFather, ddlIndustrySub, IndustryRelate,
               "","", Constants.DictionaryType.INDUSTRY_CLASS);
            //ddlAddrFather.SelectedIndex = 0;
            //ddlAddrSub.SelectedIndex = 0;
            tbLinkman.Text = "";
            tbPhone.Text = "";
            tbEmail.Text = "";
            tbRequire.Text = "";
            tbResponse.Text = "";
            lbModify.Text = "正在创建新的猎头职位...";
        }

        private void show(JobHunter job)
        {
            lbSequence.Text = Convert.ToString(job.JobId);
            tbJobNo.Text = job.JobNo;
            tbJobNo.ReadOnly = true;
            tbJobName.Text = job.Name;
            //tbIndustry.Text = job.Industry;
            WebCommon.LoadLevelDropDownList(ddlIndustryFather, ddlIndustrySub, IndustryRelate,
               Constants.GetParentCodeBySubCode(job.Industry),
               job.Industry, Constants.DictionaryType.INDUSTRY_CLASS);
            //tbSalary.Text = job.Salary;
            string salary = string.Empty;
            if (job.Salary != null && job.Salary.Length > 0) salary = job.Salary;
            WebCommon.SetDropDownFromDict(ddlSalary, salary,
                Constants.GetDictionaryCode(Constants.DictionaryType.SALARY_LEVEL), false);

            WebCommon.LoadLevelDropDownList(ddlAddrFather, ddlAddrSub, AddressRate,
                Constants.GetParentCodeBySubCode(job.OfficeAddress),
                job.OfficeAddress, Constants.DictionaryType.ADDR_CITY);

            tbLinkman.Text = job.ContactName;
            tbPhone.Text = job.ContactTel;
            tbEmail.Text = job.Email;
            tbRequire.Text = job.Grade;
            tbResponse.Text = job.Responsibility;
            lbModify.Text = job.Creator + "　(" + Functions.GetDateTimeStr(job.CreateDate.Value) + ")";
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            string id = lbSequence.Text.Trim();
            if ( id.Equals("0") ) id = "";

            JobHunter jobHunter = null;
            if ( id.Equals("") ) {
                jobHunter = new JobHunter();
            } else {
                if (jobHunter == null)
                {
                    jobHunter = logic.GetDetail(Convert.ToInt32(id));                  
                }
            }

            jobHunter.JobNo = tbJobNo.Text.Trim();
            jobHunter.Name = tbJobName.Text.Trim();
            //jobHunter.Industry = tbIndustry.Text.Trim();
            jobHunter.Industry = ddlIndustrySub.SelectedValue;
            //jobHunter.Salary = tbSalary.Text.Trim();
            jobHunter.Salary = ddlSalary.SelectedValue;
            //jobHunter.OfficeAddress = tbAddr.Text.Trim();
            jobHunter.OfficeAddress = ddlAddrSub.SelectedValue;
            jobHunter.ContactName = tbLinkman.Text.Trim();
            jobHunter.ContactTel = tbPhone.Text.Trim();
            jobHunter.Email = tbEmail.Text.Trim();
            jobHunter.Responsibility = tbResponse.Text.Trim();
            jobHunter.Grade = tbRequire.Text.Trim();

            MySession session = new MySession();
            jobHunter.Creator = session.GetAdminUserFromSession().Name;
            jobHunter.CreateDate = DateTime.Now;
            try
            {
                if (id.Equals(""))
                    logic.Insert(jobHunter);
                else
                    logic.Update(jobHunter);

                string message = message = Server.UrlEncode("保存成功，请按返回按钮查看信息!");
                string urlPath = string.Empty;
                string url = string.Empty;

                urlPath = Server.UrlEncode("~/AdminWeb/AdminMain.aspx?controlType=" + (int)Constants.SubjectType.HunterPositionList);
                url = string.Format("~/ErrorMessage.aspx?flag=1&message={0}&returnURL={1}", message, urlPath);
                Response.Redirect(url, false);

                //string url = "AdminMain.aspx?controlType="
                //   + Convert.ToString((int)Constants.SubjectType.HunterPositionList);
                //Response.Redirect(url);
            }
            catch (Exception ex)
            {
                string message = Server.UrlEncode(String.Format("保存失败，请与管理员联系！按返回按钮重新输入。错误信息: {0}!", ex.Message));
                string urlPath = Server.UrlEncode("~/AdminWeb/AdminMain.aspx?controlType=" + Convert.ToString((int)Constants.SubjectType.HunterPositionIfo));
                string url = string.Format("~/ErrorMessage.aspx?flag=0&message={0}&returnURL={1}", message, urlPath);
                Response.Redirect(url);
            }
        }

        protected void btReturn_Click(object sender, EventArgs e)
        {
            string url = "AdminMain.aspx?controlType="
               + Convert.ToString((int)Constants.SubjectType.HunterPositionList);
            Response.Redirect(url);
        }
    }
}